{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = '1'\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from generate_input_planar_pose import GenerateInputPlanarPose\n",
    "\n",
    "directory = os.path.expanduser('~/Documents/data/')\n",
    "\n",
    "generator = GenerateInputPlanarPose([\n",
    "    directory + 'cylinder-cube-1/cylinder-cube-1.db',\n",
    "    directory + 'cylinder-1/cylinder-1b.db',\n",
    "    directory + 'cube-1/cube-1.db',\n",
    "    directory + 'cylinder-1/cylinder-1a.db',\n",
    "], test_files=[\n",
    "#    directory + 'all-1/all-1.db',\n",
    "], output_folder='gen-inpaint/')\n",
    "\n",
    "#generator.generateInput({\n",
    "#    'did_grasp_weight': 0.13,\n",
    "#    'force_rewrite': True,\n",
    "#    'inpaint': True,\n",
    "#    'size_cropped': (200, 200),\n",
    "#    'size_input': (752, 480),\n",
    "#    'size_output': (32, 32),\n",
    "#})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Length: 4870\n",
      "Reward Mean: 0.20308008213552362\n",
      "Length: 19233\n",
      "Reward Mean: 0.20688400145583113\n"
     ]
    }
   ],
   "source": [
    "from tensorflow_loader import DataLoader\n",
    "\n",
    "test_loader = DataLoader(generator.test_output_filename)\n",
    "train_loader = DataLoader(generator.train_output_filename, batch_size=128)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow_utils import get_augmentation, single_class_split, tf_accuracy, tf_precision, tf_recall, tf_f1\n",
    "\n",
    "tf.reset_default_graph()\n",
    "# tf.set_random_seed(3)\n",
    "\n",
    "\n",
    "train_dataset = tf.data.Dataset.from_generator(train_loader.nextBatch, (tf.float32, tf.float32), ((None, None, None, 1), (None, 3)))\n",
    "train_dataset = train_dataset.map(get_augmentation(flip_up_down=False, flip_left_right=False, height=False, noise=False, defects=False, blur=False), num_parallel_calls=4)\n",
    "\n",
    "test_dataset = tf.data.Dataset.from_generator(test_loader.entireBatch, (tf.float32, tf.float32))\n",
    "\n",
    "\n",
    "handle = tf.placeholder(tf.string, shape=(), name='handle')\n",
    "iterator = tf.data.Iterator.from_string_handle(handle, train_dataset.output_types, train_dataset.output_shapes)\n",
    "train_iterator = train_dataset.make_initializable_iterator()\n",
    "test_iterator = test_dataset.make_initializable_iterator()\n",
    "    \n",
    "image, label = iterator.get_next()\n",
    "\n",
    "\n",
    "def act(x): return tf.nn.leaky_relu(x, 0.1)\n",
    "def reg(l1=0.0, l2=0.3): return tf.contrib.layers.l1_l2_regularizer(l1, l2)\n",
    "    \n",
    "    \n",
    "training = tf.placeholder_with_default(False, (), name='training')\n",
    "apply_dropout = tf.placeholder_with_default(training, (), name='apply_dropout')\n",
    "image = tf.identity(image, name='image')\n",
    "label = tf.identity(label, name='label')\n",
    "\n",
    "\n",
    "# Second layer with uncertainty about depth\n",
    "#h = tf.to_float(tf.equal(image, 0.0))\n",
    "#x = tf.concat([h, image], axis=3)\n",
    "    \n",
    "x = tf.layers.conv2d(image, 32, (5, 5), strides=(2, 2), activation=act, kernel_regularizer=reg(), bias_regularizer=reg())\n",
    "x = tf.layers.batch_normalization(x, training=training)\n",
    "#x = tf.layers.dropout(x, 0.4, training=apply_dropout)\n",
    "    \n",
    "x = tf.layers.conv2d(x, 48, (5, 5), strides=(1, 1), dilation_rate=(1, 1), activation=act, kernel_regularizer=reg(), bias_regularizer=reg())\n",
    "x = tf.layers.batch_normalization(x, training=training)\n",
    "#x = tf.layers.dropout(x, 0.4, training=apply_dropout)\n",
    "\n",
    "x = tf.layers.conv2d(x, 64, (5, 5), dilation_rate=(1, 1), activation=act, kernel_regularizer=reg(), bias_regularizer=reg())\n",
    "x = tf.layers.batch_normalization(x, training=training)\n",
    "x = tf.layers.dropout(x, 0.4, training=apply_dropout)\n",
    "\n",
    "x = tf.layers.conv2d(x, 142, (6, 6), dilation_rate=(1, 1), activation=act, kernel_regularizer=reg(), bias_regularizer=reg())\n",
    "# x = tf.layers.batch_normalization(x, training=training)\n",
    "x = tf.layers.dropout(x, 0.3, training=apply_dropout)\n",
    "    \n",
    "x = tf.layers.conv2d(x, 128, (1, 1), dilation_rate=(1, 1), activation=act, kernel_regularizer=reg(), bias_regularizer=reg())\n",
    "x = tf.layers.dropout(x, 0.3, training=apply_dropout)\n",
    "    \n",
    "logits = tf.layers.conv2d(x, 3, (1, 1), dilation_rate=(1, 1), bias_regularizer=reg(l2=0.2))\n",
    "prob = tf.nn.sigmoid(logits, name='prob')\n",
    "# var_prob = tf.expand_dims(tf.abs(tf.reduce_mean(tf.reduce_mean(tf.gradients(prob, [x_depth])[0], axis=2, keepdims=True), axis=1, keepdims=True)), 3, name='var_prob')\n",
    "\n",
    "    \n",
    "reward, reward_pred = single_class_split(label, prob[:, 0, 0])\n",
    "_, logits_pred = single_class_split(label, logits[:, 0, 0])\n",
    "\n",
    "weights = tf.abs(0.6 - reward) # 1.0 or tf.abs(0.75 - reward) or label[:, 2]\n",
    "loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=reward, logits=logits_pred, weights=weights)\n",
    "loss_reg = loss + tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))\n",
    "    \n",
    "accuracy = tf_accuracy(reward, reward_pred)\n",
    "precision = tf_precision(reward, reward_pred)\n",
    "recall = tf_recall(reward, reward_pred)\n",
    "f1 = tf_f1(precision, recall, beta=0.5)\n",
    "\n",
    "extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)  # For batch normalization layers\n",
    "with tf.control_dependencies(extra_update_ops):\n",
    "    train = tf.train.AdamOptimizer(learning_rate=1e-7).minimize(loss_reg, name='train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from /home/berscheid/Documents/data/cylinder-cube-1/models/model-4-inpaint\n",
      "Epoch\tTime[s]\tTrain Accuracy\tTest Loss\tAccuracy\tPrecision\tRecall\t\tF1\n",
      "   -1\t0.00\t-1.0000\t\t0.1265\t\t0.8899\t\t0.8168\t\t0.5905\t\t0.7586\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "    0\t1.34\t0.8984\t\t0.1251\t\t0.8918\t\t0.8157\t\t0.5976\t\t0.7602\n",
      "Best model yet!\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-13-bbe527465039>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     15\u001b[0m     \u001b[0mload\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m     \u001b[0msave\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m     \u001b[0mexport\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     18\u001b[0m )\n",
      "\u001b[0;32m~/Documents/bin_picking/jupyter/tensorflow_model.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, train_iterator, train_batches_per_epoch, test_iterator, epochs, early_stopping_patience, load, save, export)\u001b[0m\n\u001b[1;32m    109\u001b[0m                 \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'1'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    110\u001b[0m \u001b[0;31m#            for batch in tqdm(range(train_batches_per_epoch), unit='batches', leave=False, dynamic_ncols=True):\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 111\u001b[0;31m                 \u001b[0mtrain_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'train'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtest_metrices\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'handle:0'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain_handle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'training:0'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    112\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    113\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0monEpochEnd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mepoch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrain_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mearly_stopping_patience\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msave\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m    875\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    876\u001b[0m       result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[0;32m--> 877\u001b[0;31m                          run_metadata_ptr)\n\u001b[0m\u001b[1;32m    878\u001b[0m       \u001b[0;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    879\u001b[0m         \u001b[0mproto_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_run\u001b[0;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1098\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1099\u001b[0m       results = self._do_run(handle, final_targets, final_fetches,\n\u001b[0;32m-> 1100\u001b[0;31m                              feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[1;32m   1101\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1102\u001b[0m       \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_do_run\u001b[0;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[1;32m   1270\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1271\u001b[0m       return self._do_call(_run_fn, feeds, fetches, targets, options,\n\u001b[0;32m-> 1272\u001b[0;31m                            run_metadata)\n\u001b[0m\u001b[1;32m   1273\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1274\u001b[0m       \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_do_call\u001b[0;34m(self, fn, *args)\u001b[0m\n\u001b[1;32m   1276\u001b[0m   \u001b[0;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1277\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1278\u001b[0;31m       \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1279\u001b[0m     \u001b[0;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1280\u001b[0m       \u001b[0mmessage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[0;34m(feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[1;32m   1261\u001b[0m       \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_extend_graph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1262\u001b[0m       return self._call_tf_sessionrun(\n\u001b[0;32m-> 1263\u001b[0;31m           options, feed_dict, fetch_list, target_list, run_metadata)\n\u001b[0m\u001b[1;32m   1264\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1265\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py\u001b[0m in \u001b[0;36m_call_tf_sessionrun\u001b[0;34m(self, options, feed_dict, fetch_list, target_list, run_metadata)\u001b[0m\n\u001b[1;32m   1348\u001b[0m     return tf_session.TF_SessionRun_wrapper(\n\u001b[1;32m   1349\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moptions\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1350\u001b[0;31m         run_metadata)\n\u001b[0m\u001b[1;32m   1351\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1352\u001b[0m   \u001b[0;32mdef\u001b[0m \u001b[0m_call_tf_sessionprun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from tensorflow_model import Model\n",
    "\n",
    "model = Model(\n",
    "    test_metrices=[loss, accuracy, precision, recall, f1],\n",
    "    inputs={'image': image, 'apply_dropout': apply_dropout},\n",
    "    outputs={'prob': prob},\n",
    "    model_input_path=generator.model_directory + 'model-4-inpaint',\n",
    "    model_output_path=generator.model_directory + 'model-4-inpaint'\n",
    ")\n",
    "\n",
    "model.fit(\n",
    "    train_iterator, train_loader.batches_per_epoch, test_iterator,\n",
    "    epochs=1500,\n",
    "    early_stopping_patience=500,\n",
    "    load=True,\n",
    "    save=False,\n",
    "    export=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow_model import Model\n",
    "\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "model = Model(\n",
    "    test_metrices=[loss, accuracy, precision, recall, f1],\n",
    "    model_input_path=generator.model_directory + 'model-4-2lyr'\n",
    ")\n",
    "\n",
    "train_handle = model.sess.run(train_iterator.string_handle())\n",
    "model.sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])\n",
    "model.load()\n",
    "model.sess.run(train_iterator.initializer)\n",
    "\n",
    "\n",
    "batch_images = model.sess.run(image, feed_dict={handle: train_handle, training: True})\n",
    "image_data = np.squeeze(batch_images[0], axis=2)\n",
    "\n",
    "print(image_data.min(), image_data.max())\n",
    "plt.imshow(image_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Analysis\n",
    "\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "plt.rcParams['figure.figsize'] = (12, 9)\n",
    "\n",
    "plt.grid(True)\n",
    "plt.hist(prob_calc.flatten(), bins=np.arange(0, 1, 0.01))\n",
    "plt.hist(test_labels[:, 0], bins=[0, 0.01, 0.99, 1], alpha=0.5)\n",
    "plt.title('Histogram of the Grasp Reward Prediction of the Test Set')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def largestErrorsIn(probs, ids, labels):\n",
    "    # print(probs[np.arange(len(probs))\n",
    "    diff = np.abs(probs - labels[:len(probs), 0])\n",
    "    # diff = np.abs(probs[np.arange(len(probs)), labels[:, 1].astype(int)] - labels[:, 0])\n",
    "    print('Mean diff: ', diff.mean())\n",
    "\n",
    "    idxs = diff.argsort()[-200:][::-1]\n",
    "\n",
    "    for idx in idxs:\n",
    "        print()\n",
    "        print('ID: ', ids[idx])\n",
    "        print('Diff: ', diff[idx])\n",
    "        print(probs[idx])\n",
    "        print(labels[idx])\n",
    "        \n",
    "# largestErrorsIn(prob_calc, test_id, test_labels)\n",
    "largestErrorsIn(np.array(train_predict), train_id, train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10, 20):\n",
    "    print()\n",
    "    print(test_id[-i])\n",
    "    print(prob_calc[-i])\n",
    "    print(test_labels[-i])\n",
    "    img = mpimg.imread(test_image_names[-i])\n",
    "    plt.figure()\n",
    "    plt.title(test_id[-i])\n",
    "    plt.imshow(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
